<?php


namespace app\admin\controller;

use app\admin\model\CustomerModel;
use app\admin\model\FenPeiModel;
use app\admin\model\MessageModel;
use app\admin\service\CustomerService;
/**
 * Class Message
 * @package app\admin\controller
 * 聊天信息查询
 */

class Message extends Main{

    /**
     * @return 跳转到聊天信息页面
     */
    public function gotoMessage(){
        return $this->fetch("message");
    }

    /**
     * 加载所有的好友
     */
    public function getHaoYous(){
        $page = isset($_GET['page'])?$_GET['page']:1;
        $size = isset($_GET['size'])?$_GET['size']:10;
        $customerService =  new CustomerService();
        $users = $customerService->getUsers($page,$size);
        $result = [];
        $result["code"] = 0;
        $result["msg"] = "";
        $result["count"] = $customerService->getUserCount();
        $result["data"] = $users;
        return json($result);
    }

    /**
     * 获取用户的好友
     */
    public function getFriends(){
        $page = isset($_GET['page'])?$_GET['page']:1;
        $size = isset($_GET['size'])?$_GET['size']:10;
        $userId = isset($_GET['id'])?$_GET['id']:1;
        $role = isset($_GET['role'])?$_GET['role']:0;
        $fenPeiModel = new FenPeiModel();
        $field = "";
        if($role==0){
            $field="user_id";
        }else if($role==1){
            $field="guan_jia_id";
        }else if($role==2){
            $field="kai_fa_id";
        }else if($role==3){
            $field="admin_id";
        }
        $fenPeiInFo =  $fenPeiModel->where($field,"=",$userId)->limit(($page-1)*$size,$size)->select();
        $count = $fenPeiModel->where($field,"=",$userId)->count("id");
        //查询管理员信息
        if($fenPeiInFo!=null){
            $adminId = $fenPeiInFo[0]['admin_id'];
            $admin = CustomerModel::get($adminId);
            //查询管家信息
            $guanJiaId = $fenPeiInFo[0]['guan_jia_id'];
            $guanJia = CustomerModel::get($guanJiaId);
            //查询开发者信息
            $kaiFaId = $fenPeiInFo[0]['kai_fa_id'];
            $kaiFa = CustomerModel::get($kaiFaId);
            $users = [$admin,$guanJia,$kaiFa];
            $result = [];
            $result["code"] = 0;
            $result["msg"] = "";
            $result["count"] = $count;
            $result["data"] = $users;
            return json($result);
        }else{
            $result = [];
            $result["code"] = 400;
            $result["msg"] = "获取失败";
            $result["count"] = 0;
            $result["data"] = "";
            return json($result);
        }


    }

    /*
     * 获取聊天记录信息
     */
    public function getMessages(){
        $sendUid = isset($_GET['sid'])?$_GET['sid']:"";
        $toUid = isset($_GET['tid'])?$_GET['tid']:"";
        $page = isset($_GET['page'])?$_GET['page']:1;
        $size = isset($_GET['size'])?$_GET['size']:10;
        if(!empty($sendUid)&&!empty($toUid)){
            //查询用户头像
            $sendUser = CustomerModel::get($sendUid);
            $touUser = CustomerModel::get($toUid);
            $codes = array($sendUid,$toUid);
            sort($codes);
            $code = implode(",",$codes);
            $messageModel = new MessageModel();
            $messages = $messageModel->where("code","=",$code)->order("insetrt_time","desc")->limit(($page-1)*$size,$size)->select();
            $sendUserImg = empty($sendUser)?"http://i3.sinaimg.cn/dy/2013/0513/U5812P1DT20130513180121.jpg":$sendUser["head"];
            $toUserImg = empty($touUser)?"http://i3.sinaimg.cn/dy/2013/0513/U5812P1DT20130513180121.jpg":$touUser["head"];
            $result = [];
            $result["code"] = 0;
            $result["msg"] = "查询成功";
            $result["sendUserImg"] = $sendUserImg;
            $result["toUserImg"] = $toUserImg;
            $result["count"] = count($messages);
            $result["data"] = $messages;
            return json($result);
        }else{
            $result = [];
            $result["code"] = 0;
            $result["msg"] = "查询失败";
            $result["count"] = 0;
            $result["data"] = "";
            return json($result);
        }
    }
}